<template>
  <div>
    <el-card>
      <el-form :model="form" inline ref="form">
        <el-form-item label="企业编号" prop="eid">
          <el-input v-model="form.eid"></el-input>
        </el-form-item>
        <el-form-item label="企业名称" prop="name">
          <el-input v-model="form.name"></el-input>
        </el-form-item>
        <el-form-item label="创建者" prop="username">
          <el-input v-model="form.username"></el-input>
        </el-form-item>
        <el-form-item label="状态" prop="status">
          <el-select v-model="form.status">
            <el-option value="0" label="禁用"></el-option>
            <el-option value="1" label="启用"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="search">搜索</el-button>
          <el-button @click="reset">清除</el-button>
          <el-button type="primary" @click="add">+新增企业</el-button>
        </el-form-item>
      </el-form>
    </el-card>
    <el-card class="eList">
      <el-table :data="eList">
        <el-table-column label="序号" width="80px">
          <template slot-scope="scope">{{ pagination.pageSize*(pagination.currentPage-1)+scope.$index+1 }}</template>
        </el-table-column>
        <el-table-column label="企业编号" prop="eid" width="240px"></el-table-column>
        <el-table-column label="企业名称" prop="name" width="240px"></el-table-column>
        <el-table-column label="创建者" prop="username" width="240px"></el-table-column>
        <el-table-column label="创建日期" prop="create_time" width="240px"></el-table-column>
        <el-table-column label="状态" prop="status" width="180px">
          <template slot-scope="scope">
            <span :class="{red:scope.row.status==0}">{{ scope.row.status==0 ?'禁用':'启用'}}</span>
          </template>
        </el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-button type="text" @click="edit(scope.row)">编辑</el-button>
            <el-button @click="setStatus(scope.row.id)" type="text">{{ scope.row.status==1 ?'禁用':'启用'}}</el-button>
            <el-button type="text" @click="del(scope.row.id)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <div class="pagination">
        <el-pagination
          background
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="pagination.page"
          :page-sizes="pagination.pageSizes"
          :page-size="pagination.pageSize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="pagination.total"
        ></el-pagination>
      </div>
    </el-card>
    <addBussiness ref="addbussiness"></addBussiness>
  </div>
</template>

<script>
import { geteList,setStatus,delbussiness } from "@/api/bussiness.js";
import addBussiness from './addBussiness.vue'
export default {
  components:{
    addBussiness,
  },
  data() {
    return {
      form: {
        eid: "", //否	string	企业id
        name: "", //否	string	企业名称
        username: "", //否	string	用户名
        status: "", //否	string	状态 1（启用） 0（禁用）
      },
      pagination: {
        pageSizes: [1, 10, 20, 30],
        pageSize: 1,
        total: 0,
        currentPage: 1,
      },
      eList: [],
    };
  },
  created() {
    this.getData();
  },
  methods: {
    handleSizeChange(size) {
      this.pagination.pageSize = size;
       this.pagination.currentPage = 1
      this.getData();
    },
    handleCurrentChange(page) {
      this.pagination.currentPage = page;
      this.getData();
    },
    getData() {
      let query = {
        ...this.form,
        limit: this.pagination.pageSize,
        page: this.pagination.currentPage,
      };
      geteList(query).then((res) => {
        this.eList = res.data.data.items;
        // console.log(res);
        this.pagination.total = res.data.data.pagination.total;
      });
    },
    //搜索按钮
    search(){
      this.getData();
      this.pagination.currentPage = 1
    },
    //清除按钮
    reset(){
      this.$refs.form.resetFields()
      this.search()
    },
    //修改状态
    setStatus(id){
      setStatus({id}).then(()=>{
        this.$message.success('修改成功')
        this.getData();
      })
    },
    //删除企业
    del(id){
      this.$confirm('确定删除该企业吗？','提示',{
        confrimButtonText:'确定',
        cancelButtonText:'取消',
        type:'warning'
      }).then(()=>{
        delbussiness(id).then(()=>{
        this.$message.success('删除成功')
        this.search()
      })
      }).catch(()=>{})
    },
    //新增按钮
    add(){
      this.$refs.addbussiness.isShow = true
      this.$refs.addbussiness.mode = 'add'
    },
    //编辑企业
    edit(row){
      this.$refs.addbussiness.form = JSON.parse(JSON.stringify(row))
      this.$refs.addbussiness.isShow = true;
      this.$refs.addbussiness.mode = 'edit'
    }
  },
};
</script>

<style lang="less">
.eList {
  margin-top: 30px;
}
.pagination {
  text-align: center;
  margin-top: 30px;
}
.el-table th,
.el-table__body td {
  text-align: center;
}
.red {
  color:red;
}
</style>